package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.mmall.cart.CartFeign;
import com.mmall.dao.OrderMapper;
import com.mmall.orderItem.OrderItemFeign;
import com.mmall.product.ProductFeign;
import com.mmall.service.IOrderService;
import entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import pojo.*;
import vo.OrderItemVo;
import vo.OrderVo;
import vo.ShippingVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

@Service
public class OrderServiceImpl implements IOrderService {

    @Autowired
    private CartFeign cartFeign;
    @Autowired
    private OrderItemFeign orderItemFeign;
    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private OrderMapper orderMapper;

    public ServerResponse createOrder(Integer userId, Integer shippingId) {
        //从购物车中获取数据
        List<Cart> cartList = cartFeign.selectChkById(userId);

        //计算这个订单的总价
        ServerResponse serverResponse = this.getCartOrderItem(userId, cartList);
        if (!serverResponse.isSuccess()) {
            return serverResponse;
        }
        List<OrderItem> orderItemList = (List<OrderItem>) serverResponse.getData();
        BigDecimal payment = this.getOrderTotalPrice(orderItemList);

        //生成订单
        Order order = this.assembleOrder(userId, shippingId, payment);
        if (order == null) {
            return ServerResponse.createByErrorMessage("生成订单错误");
        }
        if (CollectionUtils.isEmpty(orderItemList)) {
            return ServerResponse.createByErrorMessage("购物车为空");
        }
        for (OrderItem orderItem : orderItemList) {
            orderItem.setOrderNo(order.getOrderNo());
        }
        //mybatis 批量插入
        orderItemFeign.batchInsert(orderItemList);

        //生成成功，要减少库存
        this.reduceProductStock(orderItemList);

        //清空一下购物车
        this.cleanCart(cartList);

        //返回给前端数据
        OrderVo orderVo = assembleOrderVo(order, orderItemList);
        return ServerResponse.createBySuccess(orderVo);

    }

    private ServerResponse getCartOrderItem(Integer userId, List<Cart> cartList) {
        List<OrderItem> orderItemList = Lists.newArrayList();
        if (CollectionUtils.isEmpty(cartList)) {
            return ServerResponse.createByErrorMessage("购物车为空");
        }
        //校验购物车的数据，包括产品的状态和数量
        for (Cart cartItem : cartList) {
            OrderItem orderItem = new OrderItem();
            Product product = productFeign.selectByPrimaryKey(cartItem.getProductId());
            //校验产品的状态
            if (Const.ProductStatusEnum.ON_SALE.getCode() != product.getStatus()) {
                return ServerResponse.createByErrorMessage("产品" + product.getName() + "不是在线售卖状态");
            }
            //校验库存
            if (cartItem.getQuantity() > product.getStock()) {
                return ServerResponse.createByErrorMessage("产品" + product.getName() + "库存不足");
            }

            orderItem.setUserId(userId);
            orderItem.setProductId(product.getId());
            orderItem.setProductName(product.getName());
            orderItem.setProductImage(product.getMainImage());
            orderItem.setCurrentUnitPrice(product.getPrice());
            orderItem.setQuantity(cartItem.getQuantity());
            orderItem.setTotalPrice(BigDecimalUtil.mul(product.getPrice().doubleValue(), cartItem.getQuantity()));
            orderItemList.add(orderItem);
        }
        return ServerResponse.createBySuccess(orderItemList);
    }

    private BigDecimal getOrderTotalPrice(List<OrderItem> orderItemList) {
        BigDecimal payment = new BigDecimal("0");
        for (OrderItem orderItem : orderItemList) {
            payment = BigDecimalUtil.add(payment.doubleValue(), orderItem.getTotalPrice().doubleValue());
        }
        return payment;
    }

    //减少库存的方法
    private void reduceProductStock(List<OrderItem> orderItemList) {
//        for (OrderItem orderItem : orderItemList) {
//            Product product = productMapper.selectByPrimaryKey(orderItem.getProductId());
//            product.setStock(product.getStock() - orderItem.getQuantity());
//            productMapper.updateByPrimaryKeySelective(product);
//        }
    }

    //清空购物车的方法
    private void cleanCart(List<Cart> cartList) {
//        for (Cart cart : cartList) {
//            cartMapper.deleteByPrimaryKey(cart.getId());
//        }
    }

    private OrderVo assembleOrderVo(Order order, List<OrderItem> orderItemList) {
        OrderVo orderVo = new OrderVo();
//        orderVo.setOrderNo(order.getOrderNo());
//        orderVo.setPayment(order.getPayment());
//        orderVo.setPaymentType(order.getPaymentType());
//        orderVo.setPaymentTypeDesc(Const.PaymentTypeEnum.codeOf(order.getPaymentType()).getValue());
//
//        orderVo.setPostage(order.getPostage());
//        orderVo.setStatus(order.getStatus());
//        orderVo.setStatusDesc(Const.OrderStatusEnum.codeOf(order.getStatus()).getValue());
//
//        orderVo.setShippingId(order.getShippingId());
//        Shipping shipping = shippingMapper.selectByPrimaryKey(order.getShippingId());
//        if (shipping != null) {
//            orderVo.setReceiverName(shipping.getReceiverName());
//            orderVo.setShippingVo(assembleShippingVo(shipping));
//        }
//
//        orderVo.setPaymentTime(DateTimeUtil.dateToStr(order.getPaymentTime()));
//        orderVo.setSendTime(DateTimeUtil.dateToStr(order.getSendTime()));
//        orderVo.setEndTime(DateTimeUtil.dateToStr(order.getEndTime()));
//        orderVo.setCreateTime(DateTimeUtil.dateToStr(order.getCreateTime()));
//        orderVo.setCloseTime(DateTimeUtil.dateToStr(order.getCloseTime()));
//
//
//        orderVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix"));
//
//
//        List<OrderItemVo> orderItemVoList = Lists.newArrayList();
//
//        for (OrderItem orderItem : orderItemList) {
//            OrderItemVo orderItemVo = assembleOrderItemVo(orderItem);
//            orderItemVoList.add(orderItemVo);
//        }
//        orderVo.setOrderItemVoList(orderItemVoList);
        return orderVo;
    }

    private Order assembleOrder(Integer userId, Integer shippingId, BigDecimal payment) {
        Order order = new Order();
        long orderNo = this.generateOrderNo();
        order.setOrderNo(orderNo);
        order.setStatus(Const.OrderStatusEnum.NO_PAY.getCode());
        order.setPostage(0);
        order.setPaymentType(Const.PaymentTypeEnum.ONLINE_PAY.getCode());
        order.setPayment(payment);

        order.setUserId(userId);
        order.setShippingId(shippingId);
        //发货时间等等
        //付款时间等等
        int rowCount = orderMapper.insert(order);
        if (rowCount > 0) {
            return order;
        }
        return null;
    }

    //组装OrderItem
    private OrderItemVo assembleOrderItemVo(OrderItem orderItem) {
        OrderItemVo orderItemVo = new OrderItemVo();
        orderItemVo.setOrderNo(orderItem.getOrderNo());
        orderItemVo.setProductId(orderItem.getProductId());
        orderItemVo.setProductName(orderItem.getProductName());
        orderItemVo.setProductImage(orderItem.getProductImage());
        orderItemVo.setCurrentUnitPrice(orderItem.getCurrentUnitPrice());
        orderItemVo.setQuantity(orderItem.getQuantity());
        orderItemVo.setTotalPrice(orderItem.getTotalPrice());

        orderItemVo.setCreateTime(DateTimeUtil.dateToStr(orderItem.getCreateTime()));
        return orderItemVo;
    }

    //组装Shipping
    private ShippingVo assembleShippingVo(Shipping shipping) {
        ShippingVo shippingVo = new ShippingVo();
        shippingVo.setReceiverName(shipping.getReceiverName());
        shippingVo.setReceiverAddress(shipping.getReceiverAddress());
        shippingVo.setReceiverProvince(shipping.getReceiverProvince());
        shippingVo.setReceiverCity(shipping.getReceiverCity());
        shippingVo.setReceiverDistrict(shipping.getReceiverDistrict());
        shippingVo.setReceiverMobile(shipping.getReceiverMobile());
        shippingVo.setReceiverZip(shipping.getReceiverZip());
        shippingVo.setReceiverPhone(shippingVo.getReceiverPhone());
        return shippingVo;
    }

    //获取订单号，订单号的规则涉及交易信息及分布式处理。
    // 此处暂且简单处理，等待后续的完善。
    private long generateOrderNo() {
        long currentTime = System.currentTimeMillis();
        return currentTime + new Random().nextInt(100);
    }
}
